import { defineStore } from 'pinia'
import { RouteRecordRaw } from 'vue-router'
import { getUserInfo, login, logout, getMenu } from '@/api/user'
import router, { filterAsyncRoutes } from '@/router'
import { getToken } from '@/utils/auth'
export interface UserState {
  token: string
  userInfo: Record<string, any>
  routes: RouteRecordRaw[]
  breadcrumb: any[]
  menu: any[]
  perms: string[]
}

const useUserStore = defineStore({
  id: 'user',
  state: (): UserState => ({
    token: getToken() || '',
    // 用户信息
    userInfo: {},
    // 路由
    routes: [],
    breadcrumb: [],
    menu: [],
    // 权限
    perms: []
  }),
  getters: {},
  actions: {
    resetState() {
      this.token = ''
      this.userInfo = {}
      this.breadcrumb = []
      this.perms = []
    },
    login(playload: any) {
      const { account, password, code, uuid } = playload
      return new Promise((resolve, reject) => {
        // login({
        //   username: account,
        //   password,
        //   code,
        //   uuid
        // })
        //   .then((data) => {
        //     this.token = data.token
        //     cache.set(TOKEN_KEY, data.token)
        //     resolve(data)
        //   })
        //   .catch((error) => {
        //     reject(error)
        //   })
      })
    },
    logout() {
      return new Promise((resolve, reject) => {
        // logout()
        //   .then(async (data) => {
        //     this.token = ''
        //     await router.push(PageEnum.LOGIN)
        //     clearAuthInfo()
        //     resolve(data)
        //   })
        //   .catch((error) => {
        //     reject(error)
        //   })
      })
    },
    getUserInfo() {
      // return new Promise((resolve, reject) => {
      //   getUserInfo()
      //     .then((data) => {
      //       this.userInfo = data.user
      //       this.perms = data.permissions
      //       resolve(data)
      //     })
      //     .catch((error) => {
      //       reject(error)
      //     })
      // })
    },
    getMenu() {
      return new Promise((resolve, reject) => {
        // getMenu()
        //   .then((data) => {
        //     this.menu = data
        //     this.routes = filterAsyncRoutes(data)
        //     resolve(data)
        //   })
        //   .catch((error) => {
        //     reject(error)
        //   })
        const data: any = [
          {
            icon: 'dashboard',
            name: '控制台',
            url: '/admin/dashboard',
            childList: []
          },
          {
            icon: 'article-manage',
            url: '/admin/article',
            name: '文章管理',
            childList: [
              {
                icon: 'article',
                url: '/admin/article/list',
                name: '文章列表',
                children: []
              },
              {
                icon: 'article-sort',
                url: '/admin/article/sort',
                name: '文章分类',
                children: []
              },
              {
                icon: 'article-tags-filled',
                url: '/admin/article/tags',
                name: '文章标签',
                children: []
              },
              {
                icon: 'recycle',
                url: '/admin/article/recycle',
                name: '回收站',
                children: []
              }
            ]
          },
          {
            icon: 'user',
            url: '/admin/user',
            name: '用户管理',
            childList: [
              {
                icon: 'user-m',
                url: '/admin/user/list',
                name: '用户列表',
                children: []
              }
            ]
          },
          {
            icon: 'system',
            name: '系统管理',
            url: '/admin/system',
            childList: [
              {
                icon: 'menu-filled',
                url: '/admin/system/menu',
                name: '菜单管理',
                children: []
              },
              {
                icon: 'notice',
                url: '/admin/system/notice',
                name: '通知公告',
                children: []
              },
              {
                icon: 'message',
                url: '/admin/system/message',
                name: '留言管理',
                children: []
              },
              {
                icon: 'website',
                url: '/admin/system/website',
                name: '网站信息',
                children: []
              },
              {
                icon: 'blogroll',
                url: '/admin/system/blogroll',
                name: '友情链接',
                children: []
              }
            ]
          }
        ]
        this.menu = data
        this.routes = filterAsyncRoutes(data)
        resolve(data)
      })
    }
  }
})

export default useUserStore
